<?php

namespace app\index\controller;

use think\Controller;

class Main extends Base {
	public function index() {
		for($i = 0; $i < 50; $i ++) {
			$roll [$i] ['user_name'] = randomkeys ( 2 ) . '***';
			$roll [$i] ['money'] = mt_rand ( 500, 49999 );
			$roll [$i] ['type'] = mt_rand ( 0, 1 ) == 0 ? lang ( 'recharge' ) : lang ( 'withdraw' );
			$roll [$i] ['create_time'] = time () - mt_rand ( 500, 5000 );
		}
		$this->assign ( 'roll', array_sort ( $roll, 'create_time', 'desc' ) );
		$rate1 = explode ( ',', lang ( 'rate1_num' ) );
		$rate2 = explode ( ',', lang ( 'rate2_num' ) );
		
		foreach ( $rate1 as $k => $v )
			$rate [] = array ('rate1' => $rate1 [$k],'rate2' => $rate2 [$k] );
		$this->assign ( 'rate', $rate );
		return $this->fetch ();
	}
	function upgrade() {
		$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$this->uid ] ) );
		if ($this->request->isPost ()) {
			$paypwd = $this->request->param ( 'paypwd' );
			if ($paypwd != $info ['paypwd'])
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'paypwd_error' ) ) );
			elseif ($info ['money'] < 5000)
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'insufficient_balance1' ) ) );
			else
				echo json_encode ( array ('code' => 99,'msg' => '' ) );
		}
	}
	function upload() {
		define ( 'WEB_ROOT', dirname ( __FILE__ ) );
		$img = $_POST ['img'];
		list ( $type, $data ) = explode ( ',', $img );
		if (strstr ( $type, 'image/jpeg' ) !== '') {
			$ext = '.jpg';
		} elseif (strstr ( $type, 'image/gif' ) !== '') {
			$ext = '.gif';
		} elseif (strstr ( $type, 'image/png' ) !== '') {
			$ext = '.png';
		}
		$dirname = date ( 'Ymd' );
		if (! is_dir ( WEB_ROOT . '/../../../public/attached/' . $dirname )) {
			mkdir ( WEB_ROOT . '/../../../public/attached/' . $dirname );
		}
		$filename = $dirname . '/' . time () . rand ( 000, 999 ) . $ext;
		$data = str_replace ( ' ', '+', $data );
		$path = WEB_ROOT . '/../../../public/attached/' . $filename;
		$res = file_put_contents ( $path, base64_decode ( $data, true ) );
		// $res = file_put_contents ( 'F:/wwwroot/obu/attached/' . $filename, $img );
		if (file_exists ( $path ))
			echo json_encode ( array ('code' => 99,'result' => $filename ) );
		else
			echo json_encode ( array ('code' => 0 ) );
	}
}
